/**
 * Copyright (c) 2019 xstgongyi.org
 * All rights reserved.
 */
package org.xstgongyi.eduaid.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xstgongyi.eduaid.dataobject.SchoolYearDO;
import org.xstgongyi.eduaid.manager.SchoolYearManager;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author oldcrane<br />
 * @created 2019-03-27
 */
@Service
public class SchoolYearServiceImpl implements SchoolYearService {

    @Autowired
    private SchoolYearManager schoolYearManager;

    @Override
    public void insert(String name, String abbrev, Boolean active) {
        // TODO 此处需要增加逻辑，检查name是否重复
        // TODO 需要增加异常处理逻辑
        SchoolYearDO schoolYear = new SchoolYearDO();
        schoolYear.setName(name);
        schoolYear.setAbbrev(abbrev);
        schoolYear.setActive(active);
        LocalDateTime now = LocalDateTime.now();
        schoolYear.setGmtCreated(now);
        schoolYear.setGmtUpdated(now);
        schoolYearManager.insert(schoolYear);
    }

    @Override
    public void update(Integer id, String name, String abbrev, Boolean active) {
        // TODO name重复检查，异常处理逻辑
        // TODO 如果传入的参数为空，忽略之
        SchoolYearDO schoolYear = new SchoolYearDO();
        schoolYear.setId(id);
        schoolYear.setName(name);
        schoolYear.setAbbrev(abbrev);
        schoolYear.setActive(active);
        schoolYear.setGmtUpdated(LocalDateTime.now());
        schoolYearManager.update(schoolYear);
        // TODO 如果no row updated，如何处理？Exception，还是忽略？
    }

    @Override
    public List<SchoolYearDO> listAll() {
        return schoolYearManager.listAll();
    }

    @Override
    public List<SchoolYearDO> listByStatus(Boolean active) {
        return schoolYearManager.listAllByStatus(active);
    }

    @Override
    public SchoolYearDO getById(Integer id) {
        return schoolYearManager.getById(id);
    }

}
